const util = require("../../utils/config.js");
//index.js
//获取应用实例
const app = getApp()
const http_host = util.http_host;
const agent_id = util.agent_id;
const img_url = util.img_url;
const appId = util.appId;
const secret = util.secret;
Page({
  data: {
    icons: [],
    title: 'index',
    canshowdata:'',
    shoptype:'',
    canshow:2,
    userInfo: null,
    userSite: '请选择位置',
    ImgUrl: img_url,
    //首页轮播图
    imgUrls: '',
    HotShop:'',
    // 附近商家-推荐商家
    recommend:'',
    //附近商家-销量商家
    sales:'',
    //附近商家-评分商家
    score:'',
    indicatorDots: false,
    autoplay: false,
    interval: 3000,
    duration: 800,
    nearShop: [],
    // tab切换
    currentTab: 0,
    winWidth: 0,
    winHeight: 0,
    myLatitude: '',
    myLongitude: '',
    //附近商家-距离最近
    DistanceShop: '',
    openId: '',
    shishidingwei:'',
  },
  //下拉刷新
  onPullDownRefresh: function () {
    var that = this
    wx.showNavigationBarLoading() //在标题栏中显示加载

    //模拟加载
    setTimeout(function () {

      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      wx.showToast({
        title: '已刷新',
        icon: 'succes',
        duration: 500,
        mask: true
      })
    }, 1000);


  },
  onLoad: function () {
    var that = this;
    wx.getLocation({

      success: function (res) {
        app.address.lat = res.latitude,
          app.address.lng = res.longitude,
        wx.request({
          url: http_host + 'indexcity',
          data: {
            lat: res.latitude,
            lng: res.longitude,
          },
          header: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            
            that.setData({
             userSite :res.data
            })
          }
        })
      }
    })
    that.data.shishidingwei=setInterval(function () {
    wx.getLocation({

      success: function (res) {
        app.address.lat = res.latitude,
          app.address.lng = res.longitude,
        wx.request({
          url: http_host + 'indexcity',
          data: {
            lat: res.latitude,
            lng: res.longitude,
          },
          header: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            
            that.setData({
             userSite :res.data
            })
          }
        })
      }
    })
    }, 1000)

    wx.request({
      url: http_host + 'canshow',
      data: {
        version: 19,
        agent_id: agent_id,
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {

        that.setData({
          canshow: res.data.a,
          canshowdata:res.data.b

        })
      }
    })

    //分类展示
    wx.request({
      url: http_host + 'type',
      data: {
        agent_id: agent_id
         //agent_id: 2
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        //console.log(1)
        //console.log(res.data)
        //console.log(that.data.icons)
        that.setData({
          shoptype: res.data,
        })


      }
    })

    //首页轮播图
    wx.request({
      url: http_host +'getCarouse',
      data: {
        agent_id: agent_id
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        that.setData({
          imgUrls: res.data,
        })
        console.log('hehe')
        console.log(res.data)
      },
    }),
    // 商家选择
    wx.request({
      url: http_host+'getHotShop',
      data: {
        agent_id : agent_id
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        that.setData({
          HotShop: res.data,
          ImgUrl: img_url
        })
      }
    })
    /**
      * 获取系统信息
      */
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        that.setData({
          winHeight: calc
        });
      }
    });
    //获取用户的上一次定位
    wx.login({
      success: function (res) {
        if (res.code) {
          wx.request({
            url: http_host + 'getMemberOpenid',
            data: {
              appId: appId,
              secret: secret,
              code: res.code
            },
            header: {
              'Content-Type': 'application/json'
            },
            success: function (res) {
              var openId = res.data.openid;
              //获取到openId以后获取
              that.setData({
                openId: res.data
              })
              wx.request({
                url: http_host + 'getBeforedistance',
                data: {
                  openId: openId,
                  order: 'data',
                  agent_id : agent_id
                },
                header: {
                  'Content-Type': 'application/json'
                },
                success: function (res) {
                  that.setData({
                    DistanceShop: res.data
                  })
                }
              })
              //获取当前用户上一次的经纬度
              wx.request({
                url: http_host + 'getPosition',
                data: {
                    openId: res.data.openid
                },
                header: {
                  'Content-Type': 'application/json'
                },
                success: function (res) {

                  if(res.data==0){
                    wx.chooseLocation({
                      success: function success(res) {
                        if (res.name.length <= 0) {
                          return that.setData({
                            userSite: res.address
                          });
                        }
                        that.setData({
                          userSite: res.name,
                          myLatitude: res.latitude,
                          myLongitude: res.longitude
                        });
                        wx.request({
                          url: http_host + 'getHotShops',
                          data: {
                            lat: that.data.myLatitude,
                            lng: that.data.myLongitude,
                            agent_id: agent_id,
                            openId: app.globalData.openId
                          },
                          header: {
                            'Content-Type': 'application/json'
                          },
                          success: function (res) {
                            that.setData({
                              DistanceShop: res.data
                            })
                          }
                        })
                      }
                    });
                  }
                  that.setData({
                    myLatitude: res.data.business_lat,
                    myLongitude: res.data.business_lng,
                  })
                }
              })
            }
          })
        }
      }
    })
     var openId = app.globalData.openId;
    //获取推荐列表
    wx.request({
      url: http_host + 'getBeforedistance',
      data: {
        openId: openId,
        order: 'recommend',
        agent_id: agent_id
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        that.setData({
          recommend: res.data
        })
      }
    })
    //获取销售列表
    wx.request({
      url: http_host + 'getBeforedistance',
      data: {
        openId: openId,
        order: 'goods',
        agent_id: agent_id
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        that.setData({
          sales: res.data
        })
      }
    })
    //获取当前评分列表
    wx.request({
      url: http_host + 'getBeforedistance',
      data: {
        openId: openId,
        order: 'score',
        agent_id: agent_id
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        that.setData({
          score: res.data
        })
      }
    })
  },
  onScroll: function (e) {
    if (e.detail.scrollTop > 100 && !this.data.scrollDown) {
      this.setData({
        scrollDown: true
      });
    } else if (e.detail.scrollTop < 100 && this.data.scrollDown) {
      this.setData({
        scrollDown: false
      });
    }
  },

  fenlei:function (e)
  {

    if (e.currentTarget.dataset.id == 1){
      wx.navigateTo({
        url: "../youhuiquan/youhuiquan"
      })
    }else if (e.currentTarget.dataset.id == 2) {
      wx.navigateTo({
        url: "../reservation/reservation"
      })
    }else if (e.currentTarget.dataset.id == 3) {
      wx.navigateTo({
        url: "../showShop/showShop?type=0"
      })
    }else if (e.currentTarget.dataset.id == 4) {
      wx.navigateTo({
        url: "../showShop/showShop?type=1"
      })
    }else{
      wx.navigateTo({
        url: "../showShop/showShop?fenlei=1&type=" + e.currentTarget.dataset.id
      })

    }
  },
  /**
   * 用户选择位置
   * @returns {boolean}
   */
  chooseLocation: function chooseLocation() {
    var that = this;
    wx.chooseLocation({
      success: function success(res) {
        if (res.name.length <= 0) {
          return that.setData({
            userSite: res.address
          });
        }
        that.setData({
          userSite: res.name,
          myLatitude: res.latitude,
          myLongitude:res.longitude
        });
        wx.request({
          url: http_host+'getHotShops',
          data: {
            lat: that.data.myLatitude,
            lng: that.data.myLongitude,
            agent_id : agent_id,
            openId: app.globalData.openId
          },
          header: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            that.setData({
              DistanceShop: res.data
            })
            clearInterval(that.data.shishidingwei);
          }
        })
      }
    });
  },

  /**
   * 用户搜索
   */
  goSearch: function goSearch() {
    wx.navigateTo({
      url: '../search/search'
    });
  },
  /**
     * 滑动切换tab
     */
  bindChange: function (e) {

    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
    this.checkCor();

  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  /**
   * 点击tab切换
   */
  swichNav: function (e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '吃货点餐',
      desc: '吃货点餐',
      path: '/pages/index/index'
    }
  },

  //首页地区自动选择
  /**
   * 点击tab切换
   */
  swichNav: function (e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  }

});



